package com.zk.code.net;

import android.content.ContentResolver;
import android.content.Context;
import android.media.audiofx.PresetReverb;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.telephony.TelephonyManager;
import android.provider.Settings;
import com.example.imgrecognition.MyApplication;

import java.util.UUID;

/**
 * Created by Administrator on 2018/9/7.
 */
public class SerialNumber {

    public static String getDeviceNo(){
        String imei = "0";
        String macAddress = "";
        Context context = MyApplication.instance.getApplicationContext();
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
        if (telephonyManager != null) {
            imei = telephonyManager.getDeviceId();
        }
        if(imei==null || imei.equals("")){
            WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            if (wifiManager != null) {
                imei = wifiManager.getConnectionInfo().getMacAddress();
            }
        }
        if(imei==null || imei.equals("")) {
            String androidID = Settings.System.getString(context.getContentResolver(), Settings.System.ANDROID_ID);
            imei = androidID + Build.SERIAL;
        }
        if(imei==null || imei.equals("")) {
            imei = UUID.randomUUID().toString();
        }

        return imei;
    }

    public static String generateUniqueDeviceId(){
        Context context = MyApplication.instance.getApplicationContext();
        String imei = "";
        String androidId = "";
        String macAddress = "";

        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(context.TELEPHONY_SERVICE);
        if (telephonyManager != null) {
            imei = telephonyManager.getDeviceId();
        }
        ContentResolver contentResolver = context.getContentResolver();
       /* if (contentResolver != null) {
            androidId = Settings.Secure.getString(contentResolver, Settings.Secure.ANDROID_ID);
        }*/
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        if (wifiManager != null) {
            macAddress = wifiManager.getConnectionInfo().getMacAddress();
        }

        StringBuilder longIdBuilder = new StringBuilder();
        if (imei != null) {
            longIdBuilder.append(imei);
        }
        if (androidId != null) {
            longIdBuilder.append(androidId);
        }
        if (macAddress != null) {
            longIdBuilder.append(macAddress);
        }
        return longIdBuilder.toString();
    }

}
